package com.thtf.openguess.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Stopwatch;
import com.thtf.openguess.aop.annotation.Time;
import com.thtf.openguess.mapper.C2016000000B20411Mapper;
import com.thtf.openguess.openguess.C2016000000B20411;
import com.thtf.openguess.service.AsyncService;
import com.thtf.openguess.service.C2016000000B20411Service;
import com.thtf.openguess.util.Util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author auth
 * @since 2022-05-27
 */
@Service
public class C2016000000B20411ServiceImpl extends ServiceImpl<C2016000000B20411Mapper, C2016000000B20411> implements C2016000000B20411Service {



    @Resource
    private AsyncService asyncService;

    @Override
    @Time
    public String insertLonely(Integer num) {
        for (int i = 0; i < num; i++) {
            C2016000000B20411 c2016000000B20411 = Util.forEachSetValue(new C2016000000B20411());
            this.save(c2016000000B20411);
        }
        return null;
    }

    @Override
    @Time
    public String insertBatch(Integer num) {
        List<C2016000000B20411> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            C2016000000B20411 c2016000000B20411 = Util.forEachSetValue(new C2016000000B20411());
            list.add(c2016000000B20411);
        }
        this.saveBatch(list);
        return null;
    }

    @Override
    @Time
    public String insertThread(Integer sqlNum, Integer threadNum) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        Stopwatch started = Stopwatch.createStarted();
        for (int i = 1; i <= threadNum; i++) {
            // 第j次提交步长
            List<C2016000000B20411> list = new ArrayList<>();
            for (int j = 1; j <= sqlNum; j++) {
                C2016000000B20411 c2016000000B20411 = Util.forEachSetValue(new C2016000000B20411());
                list.add(c2016000000B20411);
            }
            // 添加执行SQL
            asyncService.dealAdd(list, countDownLatch, sqlNum, this);

        }
        countDownLatch.await();
        return null;
    }

    @Override
    @Time
    public String insertZzjgdm(Integer sqlNum, String zzjgdm) {
        List<C2016000000B20411> list = new ArrayList<>();
        for (int i = 0; i < sqlNum; i++) {
            C2016000000B20411 c2016000000B20411 = Util.forEachSetValue(new C2016000000B20411());
            c2016000000B20411.setZzjgdm(zzjgdm);
            list.add(c2016000000B20411);
        }
        this.saveBatch(list);
        return null;
    }

    @Override
    @Time
    public String updateSingle(String zzjgdm, Integer type) {
        Collection<C2016000000B20411> c2016000000B20411s = this.selectList(zzjgdm);
        c2016000000B20411s.forEach(c2016000000B20411 -> {
            C2016000000B20411 c2016000000B20411A = new C2016000000B20411();
            c2016000000B20411A.setBgq(c2016000000B20411.getBgq());
            switch (type) {
                case 1:
                    this.addLess(c2016000000B20411A);
                    break;
                case 2:
                    this.addMore(c2016000000B20411A);
                    break;
                case 3:
                    this.addAll(c2016000000B20411A);
                    break;
            }
            this.updateById(c2016000000B20411A);
        });

        return null;
    }

    @Override
    @Time
    public String updatebatch(String zzjgdm, Integer type) {
        Collection<C2016000000B20411> c2016000000B20411s = this.selectList(zzjgdm);
        List<C2016000000B20411> list = new ArrayList<>();
        c2016000000B20411s.forEach(c2016000000B20411 -> {
            C2016000000B20411 c2016000000B20411A = new C2016000000B20411();
            c2016000000B20411A.setBgq(c2016000000B20411.getBgq());
            switch (type) {
                case 1:
                    this.addLess(c2016000000B20411A);
                    break;
                case 2:
                    this.addMore(c2016000000B20411A);
                    break;
                case 3:
                    this.addAll(c2016000000B20411A);
                    break;
            }
            list.add(c2016000000B20411A);
        });
        this.updateBatchById(list);
        return null;
    }

    @Override
    @Time
    public String deleteSingle(String zzjgdm) {
        Collection<C2016000000B20411> c2016000000B20411s = this.selectList(zzjgdm);
        c2016000000B20411s.forEach(c2016000000B20411 -> {
            this.removeById(c2016000000B20411.getBgq());
        });
        return null;
    }

    @Override
    @Time
    public String deleteBatch(String zzjgdm) {
        Collection<C2016000000B20411> c2016000000B20411s = this.selectList(zzjgdm);
        List<String> list = new ArrayList<>();
        c2016000000B20411s.forEach(c2016000000B20411 -> {
            list.add(c2016000000B20411.getBgq());
        });
        this.removeByIds(list);
        return null;
    }

    private void addLess(C2016000000B20411 c2016000000B20411A) {
        c2016000000B20411A.setA001(Util.ramdom().toString());
        c2016000000B20411A.setA002(Util.ramdom().toString());
        c2016000000B20411A.setA003(Util.ramdom().toString());
        c2016000000B20411A.setA004(Util.ramdom().toString());
    }

    private void addMore(C2016000000B20411 c2016000000B20411A) {
        this.addLess(c2016000000B20411A);
        c2016000000B20411A.setA005(Util.ramdom().toString());
        c2016000000B20411A.setA006(Util.ramdom().toString());
        c2016000000B20411A.setC01_1(Util.ramdom());
        c2016000000B20411A.setC01_2(Util.ramdom());
        c2016000000B20411A.setC01_3(Util.ramdom());
        c2016000000B20411A.setC01_4(Util.ramdom());
        c2016000000B20411A.setC03_1(Util.ramdom());

    }

    private void addAll(C2016000000B20411 c2016000000B20411A) {
        C2016000000B20411 c2016000000B20411 = Util.forEachSetValue(new C2016000000B20411());
        c2016000000B20411.setBgq(c2016000000B20411A.getBgq());
        c2016000000B20411.setZzjgdm(c2016000000B20411A.getZzjgdm());
        c2016000000B20411.setDcdxxtm(c2016000000B20411A.getDcdxxtm());
        BeanUtils.copyProperties(c2016000000B20411, c2016000000B20411A);
    }

    private Collection<C2016000000B20411> selectList(String zzjgdm) {
        Map<String, Object> map = new HashMap<>();
        map.put("zzjgdm", zzjgdm);
        return this.listByMap(map);
    }


}
